package com.blb.mq02.mq022;

import com.blb.util.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;

/**
 * 多对多模式的消费者1
 */
public class WorkConsumer02 {

    public static void main(String[] args) throws IOException, InterruptedException {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(MQUtils.QUEUE_NAME,false,false,false,null);
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        //同一时刻服务器只发送一条消息给消费者
        channel.basicQos(1);
        //消费者消费通道中的消息 false代表手动确认
        channel.basicConsume(MQUtils.QUEUE_NAME,false,queueingConsumer);
        while(true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            System.out.println("WorkConsumer2 receive :" + new String(delivery.getBody()));
            Thread.sleep(1000);
            //手动确定返回状态，不写就是自动确认
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
        }
    }
}
